package cn.ad.server.web.spring.service.transmitter;

import cn.ad.server.web.spring.model.AdResponse;
import cn.ad.server.web.spring.model.ITransmitInfo;
import cn.ad.server.web.spring.service.components.http.client.AdHttpExecutorUtil;
import cn.ad.server.web.spring.service.components.http.client.AdHttpResponse;
import cn.ad.server.web.spring.service.components.http.client.ResponseHandleException;
import cn.ad.server.web.spring.service.httpclient4.ByteArrayWrapStream;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * 测试2
 * 
 * @author wangwei
 *
 */
public class TestTransmitter2 extends AbstractBaseTransmitter {

	static final Logger LOGGER = LoggerFactory.getLogger(TestTransmitter2.class);

	private String url;
	private String ch;

	@Override
	public String getUrl() {
		return this.url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getCh() {
		return ch;
	}

	public void setCh(String ch) {
		this.ch = ch;
	}
	
    @Resource(name="httpClientExecutorUtil")
    private AdHttpExecutorUtil adHttpExecutorUtil;

	
	@Override
	public AdResponse transmit(ITransmitInfo info) {
		AdResponse resp = new AdResponse(404){};
		try {
			AdHttpResponse<ByteArrayWrapStream> adHttpResponse = adHttpExecutorUtil.doGetExecute(getUrl()+ "?" + info.getTransmitInfo(), null, -1);
			if(adHttpResponse.getStatusCode()==HttpStatus.SC_OK){
				ByteArrayWrapStream  byteArrayWrapStream = adHttpResponse.getResult();
				LOGGER.debug(byteArrayWrapStream.toString(Charset.forName("utf-8")));
			}
			resp.setStatus(adHttpResponse.getStatusCode());
		} catch (ResponseHandleException | IOException e) {
			LOGGER.error("第三方补量渠道请求失败。",e);
		}
		return resp;
	}
	
}
